package com.qianbao.qianbaobusiness.ui.cashier;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.androidbase.widget.recycleadpter.BaseRecycleViewAdapter;
import com.baseandroid.config.Constant;
import com.baseandroid.config.Global;
import com.qianbao.qianbaobusiness.BuildConfig;
import com.qianbao.qianbaobusiness.R;
import com.qianbao.qianbaobusiness.json.DataListBean;
import com.tencent.stat.StatService;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 收银明细列表适配器
 */
public class CashierListAdapter extends BaseRecycleViewAdapter<DataListBean> {

    public CashierListAdapter(Context context) {
        super(context);
    }

    @Override
    protected void onBindBaseViewHolder(RecyclerView.ViewHolder holder, DataListBean item) {
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.itemTime.setText(item.getTradeDate());       //下单时间
        viewHolder.itemCashier.setText(item.getTradeAmt());     //实付金额

        viewHolder.itemCashierBuy.setText(mContext.getResources().getString(R.string.consumption)
                + item.getOrderAmt());  //订单金额
        switch (item.getPayType()){     //付款类型
            case Constant.ORDER_TYPE_CARD :     //银行卡支付
                viewHolder.iconType.setImageResource(R.drawable.icon_paytype_card);
                break;

            case Constant.ORDER_TYPE_ZHIFUBAO :     //支付宝
                viewHolder.iconType.setImageResource(R.drawable.icon_paytype_zhifubao);
                break;

            case Constant.ORDER_TYPE_WEIXIN :   //微信
                viewHolder.iconType.setImageResource(R.drawable.icon_paytype_weixin);
                break;

            case Constant.ORDER_TYPE_QIANBAO :  //钱包支付
                viewHolder.iconType.setImageResource(R.drawable.icon_paytype_qianbao);
                break;

            case Constant.ORDER_TYPE_BHH :  //白花花支付
                    viewHolder.tvBhh.setVisibility(View.VISIBLE);
                break;
        }

        switch (item.getTradeStatus()){
            case Constant.TRADE_SUCCESS :   //交易成功
                viewHolder.itemCashierBuy.setText(mContext.getResources().getString(R.string.consumption)
                        + "+" +item.getOrderAmt());  //订单金额
                    viewHolder.itemCashier
                            .setTextColor(ContextCompat.getColor(mContext, R.color.text_red));
                    viewHolder.itemStatus
                            .setTextColor(ContextCompat.getColor(mContext, R.color.text_support));
                    viewHolder.itemStatus
                            .setText(mContext.getResources().getString(R.string.trade_success));
                break;

            case Constant.TRADE_FAIL :   //交易失败
                    viewHolder.itemCashier
                            .setTextColor(ContextCompat.getColor(mContext, R.color.text_support));
                    viewHolder.itemStatus
                            .setTextColor(ContextCompat.getColor(mContext, R.color.c_FFA800));
                    viewHolder.itemStatus
                            .setText(mContext.getResources().getString(R.string.trade_fail));
                break;

            case Constant.TRADE_UNDO :   //交易撤销
                    viewHolder.itemCashier
                            .setTextColor(ContextCompat.getColor(mContext, R.color.text_support));
                    viewHolder.itemStatus
                            .setTextColor(ContextCompat.getColor(mContext, R.color.c_FFA800));
                    viewHolder.itemStatus
                            .setText(mContext.getResources().getString(R.string.trade_undo));
                break;

            case Constant.TRADE_DEALING :   //交易处理中
                    viewHolder.itemCashier
                            .setTextColor(ContextCompat.getColor(mContext, R.color.text_support));
                    viewHolder.itemStatus
                            .setTextColor(ContextCompat.getColor(mContext, R.color.c_FFA800));
                    viewHolder.itemStatus
                            .setText(mContext.getResources().getString(R.string.trade_dealing));
                break;

            case Constant.TRADE_IMPACT :   //交易已冲正
                    viewHolder.itemCashier
                            .setTextColor(ContextCompat.getColor(mContext, R.color.text_support));
                    viewHolder.itemStatus
                            .setTextColor(ContextCompat.getColor(mContext, R.color.c_FFA800));
                    viewHolder.itemStatus
                            .setText(mContext.getResources().getString(R.string.trade_impact));
                break;

            case Constant.TRADE_IMPACT_FAIL :   //交易冲正失败
                    viewHolder.itemCashier
                            .setTextColor(ContextCompat.getColor(mContext, R.color.text_support));
                    viewHolder.itemStatus
                            .setTextColor(ContextCompat.getColor(mContext, R.color.c_FFA800));
                    viewHolder.itemStatus
                            .setText(mContext.getResources().getString(R.string.trade_impact_fail));
                break;

            case Constant.TRADE_UNDO_SUCCESS :   //交易撤销成功
                    viewHolder.itemCashier
                            .setTextColor(ContextCompat.getColor(mContext, R.color.text_support));
                    viewHolder.itemStatus
                            .setTextColor(ContextCompat.getColor(mContext, R.color.c_FFA800));
                    viewHolder.itemStatus
                            .setText(mContext.getResources().getString(R.string.trade_undo_success));
                break;

            case Constant.TRADE_UNDO_FAIL :   //交易撤销失败
                    viewHolder.itemCashier
                            .setTextColor(ContextCompat.getColor(mContext, R.color.text_support));
                    viewHolder.itemStatus
                            .setTextColor(ContextCompat.getColor(mContext, R.color.c_FFA800));
                    viewHolder.itemStatus
                            .setText(mContext.getResources().getString(R.string.trade_undo_fail));
                break;
        }

        viewHolder.itemLines.setVisibility(viewHolder.getAdapterPosition() == getItemCount() - 1 ?
                             View.INVISIBLE : View.VISIBLE);
        if (Global.getShopListInfo().merchantList.size() == 1) {
            viewHolder.itemOrderno.setVisibility(View.INVISIBLE);
            viewHolder.itemMerchantname.setEllipsize(TextUtils.TruncateAt.MIDDLE);
            viewHolder.itemMerchantname.setText(item.getOrderNo());
        } else {
            viewHolder.itemMerchantname.setEllipsize(TextUtils.TruncateAt.END);
            viewHolder.itemOrderno.setVisibility(View.VISIBLE);
            viewHolder.itemOrderno.setText(item.getOrderNo());
            for (int i = 0; i < Global.getShopListInfo().merchantList.size(); i++) {
                if (TextUtils.equals(item.getMerchantNo(), Global.getShopListInfo()
                        .merchantList.get(i).merchantNo)) {
                    viewHolder.itemMerchantname.setText(item.getMerchantName());
                    break;
                }
            }

        }

        /**
         * view的点击事件
         */
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, CashierDetailActivity.class);
                intent.putExtra("bean",item);
                mContext.startActivity(intent);
                if (!BuildConfig.DEBUG) {
                    StatService.trackCustomEvent(mContext, "zy_cashier_cash_recently");
                }
            }
        });

    }

    @Override
    protected RecyclerView.ViewHolder onCreateBaseViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_cashier_list,null);
        return new ViewHolder(view);
    }


    @Override
    protected int getBaseItemViewType(int position) {
        return 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.icon_type)
        ImageView iconType ;    //支付类型图标
        @BindView(R.id.item_merchantname)
        TextView itemMerchantname ;     //店铺名称
        @BindView(R.id.item_time)
        TextView itemTime ;     //交易时间
        @BindView(R.id.item_orderno)
        TextView itemOrderno ;  //订单号
        @BindView(R.id.item_status)
        TextView itemStatus ;   //交易状态
        @BindView(R.id.item_cashier)
        TextView itemCashier ;  //实付金额
        @BindView(R.id.item_cashier_buy)
        TextView itemCashierBuy ;   //交易金额
        @BindView(R.id.tv_bhh)
        TextView tvBhh ;    //白花花支付
        @BindView(R.id.item_lines)
        View itemLines ;    //隔条

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
